<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.transcargo.data.CountriesData" %>
<%@ page import="com.transcargo.geo.Country" %>
<%@ page import="com.transcargo.geo.Region" %>
<%@ page import="com.transcargo.geo.City" %>
<%@ page import="com.transcargo.data.Cache" %>
<%@ page import="java.util.Date" %>
<%@include file="header.jsf"%>
<%@include file="session.jsf"%>
<%@include file="resolve_country.jsf"%>
<%
	Date date = new Date();
	String id = request.getParameter("cid");
	if (id == null || id.length() == 0) {
		response.sendRedirect("countries_freight.jsp");
		return;
	}
	int Id = Integer.parseInt(id);
	Country country = Cache.getCountry(Id);
	Boolean showRegions = country.getNumRegions() > 0;

%>
<html>
  	<head><title>TransCargo</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link type="text/css" href="transcargo.css" rel="stylesheet"/>

  </head>
  <body>
    <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tr><td colspan="2">
			<%@include file="page_header.jsf"%>
        </td></tr>
        <tr><td width="80%" align="center">
            <table width="100%" cellpadding="0" cellspacing="0">
	            <tr><td align="center"><h1>ТрансКарго</h1></td></tr>
	            <tr><td><center>Поиск транспорта на текущую дату<h2><%=country.getName() %></h2><br>
				<table border="0">
				<tr><th<%=showRegions?" colspan=2":"" %>>&nbsp;</th><th>Ожидает загрузки</th><th>Разгружается</th></tr>
		<%
				if (showRegions) 
					for (int i = 0; i < country.getNumRegions(); i++) {
						Region region = country.getRegions()[i];
						list = dbase.execute("SELECT count(id) FROM posted_transport WHERE visible=1 AND ((date_load<=CURRENT_DATE AND date_unload>=CURRENT_DATE) OR permanent=1) AND country_id_from="+country.getId()+" AND region_id_from=" + region.getId());
						iter = list.iterator();
						String[] res = iter.next();
						int cargo_from = Integer.parseInt(res[0]);
						list = dbase.execute("SELECT count(id) FROM posted_transport WHERE visible=1 AND ((date_load<=CURRENT_DATE AND date_unload>=CURRENT_DATE) OR permanent=1) AND country_id_to="+country.getId()+" AND region_id_to=" + region.getId());
						iter = list.iterator();
						res = iter.next();
						int cargo_to = Integer.parseInt(res[0]);
						%><tr><td colspan="2"><%=region.getName() %></td><td align="center"><a href="search_freight.jsp?cid_from=<%=Id%>&rid_from=<%=region.getId()%>"><%=cargo_from %></a></td><td align="center"><a href="search_freight.jsp?cid_to=<%=Id%>&rid_to=<%=region.getId()%>"><%=cargo_to %></a></td></tr><%
						for (int j = 0; j < country.getNumCities(); j++) {
							City city = country.getCities()[j];
							if (city.getRegionId() == region.getId()) {
								list = dbase.execute("SELECT count(id) FROM posted_transport WHERE visible=1 AND ((date_load<=CURRENT_DATE AND date_unload>=CURRENT_DATE) OR permanent=1) AND country_id_from="+country.getId()+" AND city_id_from=" + city.getId());
								iter = list.iterator();
								res = iter.next();
								cargo_from = Integer.parseInt(res[0]);
								list = dbase.execute("SELECT count(id) FROM posted_transport WHERE visible=1 AND ((date_load<=CURRENT_DATE AND date_unload>=CURRENT_DATE) OR permanent=1) AND country_id_to="+country.getId()+" AND city_id_to=" + city.getId());
								iter = list.iterator();
								res = iter.next();
								cargo_to = Integer.parseInt(res[0]);
								%><tr><td width="15">&nbsp;</td><td><%=city.getName() %></td><td align="center"><a href="search_freight.jsp?cid_from=<%=Id%>&rid_from=<%=region.getId()%>&ctid_from=<%=city.getId()%>"><%=cargo_from %></a></td><td align="center"><a href="search_freight.jsp?cid_to=<%=Id%>&rid_to=<%=region.getId()%>&ctid_to=<%=city.getId()%>"><%=cargo_to %></a></td></tr><%
							
							}
						}
					} else for (int i = 0; i < country.getNumCities(); i++) {
						City city = country.getCities()[i];
						list = dbase.execute("SELECT count(id) FROM posted_transport WHERE visible=1 AND ((date_load<=CURRENT_DATE AND date_unload>=CURRENT_DATE) OR permanent=1) AND country_id_from="+country.getId()+" AND city_id_from=" + city.getId());
						iter = list.iterator();
						String[] res = iter.next();
						int cargo_from = Integer.parseInt(res[0]);
						list = dbase.execute("SELECT count(id) FROM posted_transport WHERE visible=1 AND ((date_load<=CURRENT_DATE AND date_unload>=CURRENT_DATE) OR permanent=1) AND country_id_to="+country.getId()+" AND city_id_to=" + city.getId());
						iter = list.iterator();
						res = iter.next();
						int cargo_to = Integer.parseInt(res[0]);
						%><tr><td><%=city.getName() %></td><td align="center"><a href="search_freight.jsp?cid_from=<%=Id%>&ctid_from=<%=city.getId()%>"><%=cargo_from %></a></td><td align="center"><a href="search_freight.jsp?cid_to=<%=Id%>&ctid_to=<%=city.getId()%>"><%=cargo_to %></a></td></tr><%
					
					}

		
		%></table>
        </td><td valign="top"><%@include file="login.jsf"%><br><%@include file="quicksearch.jsf"%></td></tr>
    </table>
  </body>
</html>
